/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.cen.disp;

import core.para.Para;
import gui.VTL1;
import gui.processor.Processor;
import gui.utils.Borders;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import javax.swing.BorderFactory;
import javax.swing.JPanel;

/**
 *
 * @author jarcher
 */
class SeqPane extends JPanel {

    private Graphics2D bg = null;
    private Image offScreen = null;
    private VTL1 vtl1;

    public SeqPane(VTL1 vtl1) {
        this.vtl1 = vtl1;
        this.setPreferredSize(new Dimension(vtl1.getWidthOfApp() - 4, vtl1.getHeight()));
        this.setBackground(new Color(102, 102, 102));
        this.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
        this.repaint();
    }

    public void paintComponent(Graphics g) {
        g.setColor(Color.black);
        displayReads(g);
    }

    public void updateReads() {
        this.repaint();
    }

    public static int TITLE_SPACE = 22;
    private void displayReads(Graphics g) {
        int noToShow = Para.END_VERTICAL_VIEW - Para.START_VERTICAL_VIEW;
        final int imageWidth = vtl1.getWidthOfApp();
        int imageHeightVal = Borders.SEQCHARHEIGHT * noToShow;
        if (imageHeightVal == 0) {
            imageHeightVal = 1;
        }
        final int imageHeight = imageHeightVal;
        this.setLayout(new FlowLayout() {
            public Dimension preferredLayoutSize(Container cont) {
                return (new Dimension(imageWidth, imageHeight));
            }
        });
        offScreen = this.createImage(imageWidth, imageHeight);
        bg = (Graphics2D) offScreen.getGraphics();
        bg.setColor(new Color(102, 102, 102));
        bg.fillRect(0, 0, imageWidth, imageHeight);
        bg.setFont(Borders.getFont(Borders.SEQFONT));

        int YIndex = 1;
        int sNo = Para.START_VERTICAL_VIEW;
        int counter = 0;
        while (counter < noToShow && sNo < Processor.tScripts.seqs.size()) {
            int ylocation = YIndex * Borders.SEQCHARHEIGHT;
            bg.setColor(new Color(178, 178, 178));
            int xPosToDisplay = 0;

            String title = Processor.tScripts.seqs.get(sNo).title;
            if(title.length() > TITLE_SPACE){
                title = title.substring(YIndex, TITLE_SPACE-2)+"..";
            }
            else{
                while(title.length()<TITLE_SPACE){title+=" ";}
            }
            title += " :";
            
            bg.drawString(
                    title,
                    (xPosToDisplay * Borders.SEQ_CHAR_WIDTH),
                    ylocation);

            xPosToDisplay = title.length();

            

            for (int x = Para.START_HORIZONTAL_VIEW; x < Para.END_HORIZONTAL_VIEW && x < Processor.tScripts.seqs.get(sNo).dna.length() - Para.START_HORIZONTAL_VIEW; x++) {
                bg.drawString(
                        Processor.tScripts.seqs.get(sNo).dna.charAt(x + Para.START_HORIZONTAL_VIEW) + "",
                        (xPosToDisplay * Borders.SEQ_CHAR_WIDTH),
                        ylocation);
                xPosToDisplay++;
            }

            YIndex++;
            counter++;
            sNo++;
        }
        g.drawImage(offScreen, 0, 0, imageWidth, imageHeight, this);
    }
}
